const xlsx = require('node-xlsx');
const fs = require('fs')
const path = require('path')
// 创建表格
function create(data) {
    try {
        var _buffer = xlsx.build(data);
        const name = path.join('public/test.xlsx')
        fs.writeFileSync(name, _buffer);
        return name
    } catch (error) {
        throw error
    }
}

//  生成表格数据
function makeSheetData(sheetMap, format) {
    let transform = function (data) {
        return data;
    }
    let datas = sheetMap,
        sheetData = [];
    datas.titleLine.forEach(function (item, index) {
        datas.titleLine[index] = item.replace(/^[a-zA-Z0-9]+-[a-zA-Z0-9]+-/, '');
    });
    sheetData.push(['UserID'].concat(datas.titleLine));
    for (let user in datas.sheetContent) {
        if (datas.sheetContent.hasOwnProperty(user)) {
            let userInfos = datas.sheetContent[user],
                row = Array(datas.titleLine.length).fill(0);
            row[0] = user;
            userInfos.forEach(function (item, index) {
                row[item.cellIndex] = transform(item.value);
            });

            sheetData.push(row);
        }
    }
    return sheetData;
}
module.exports = {
    makeSheetData,
    create
}
